package com.toobigtofail.enitities
{
	import flash.display.Sprite;
	
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.Graphic;
	import net.flashpunk.Mask;
	import net.flashpunk.Tween;
	import net.flashpunk.graphics.Spritemap;
	import net.flashpunk.tweens.motion.QuadPath;
	import net.flashpunk.utils.Ease;
	
	
	public class Bat extends Entity
	{
		[Embed(source="/assets/gfx/batstrip.png")] private static const BAT_PNG:Class;
		
		
		private static const BATSHIT_TIME:Number = 1.5;
		
		public var sprBat:Spritemap = new Spritemap(BAT_PNG,32,26);
		public var batPathTween:QuadPath = new QuadPath(endFlight);
		public var shitTimer:Number = 0;
		
		public function Bat()
		{
			sprBat.add("fly", [0,1,2,1], 6, true);
			
			
			graphic = sprBat;
			
			addTween(batPathTween);
			
			setBatPath();
			
		}
		
		public function endFlight():void
		{
			FP.world.remove(this);
		}
		
		private function setBatPath():void
		{
			
			var spawnRand:Number = Math.random();
			var spawnSide:Number = 0;
			if(Math.floor(spawnRand * 100)%2 == 1)
				spawnSide = FP.screen.width+30;
			else
				spawnSide = -30;
			
			var spawnHeight:Number = (FP.screen.height /6)* spawnRand;
			
			if(spawnSide < 0)
			{
				batPathTween.addPoint(spawnSide, spawnHeight)
				batPathTween.addPoint(FP.width / 3, FP.height/7);
				batPathTween.addPoint(2 * FP.width / 3, FP.height/6);
				batPathTween.addPoint(FP.width + 30, 0);
			}
			else
			{
				batPathTween.addPoint(spawnSide, spawnHeight)
				batPathTween.addPoint(2 * FP.width / 3, FP.height/6);
				batPathTween.addPoint(FP.width / 3, FP.height/7);
				batPathTween.addPoint(-30, 0);
				
			}
			batPathTween.setMotion(5, Ease.quadInOut);
			
		}
		
		override public function added():void
		{
			batPathTween.start();
			
		}
		
		override public function update():void
		{
			super.update();
			this.x = batPathTween.x;
			this.y = batPathTween.y;
			
			if(shitTimer > BATSHIT_TIME)
			{
				shit();
				shitTimer = 0;
			}
			else
			{
				shitTimer += FP.elapsed;
			}
			animate();
		}
		
		public function shit():void
		{
			FP.world.add(new BatShit(this.x,this.y));
		}
		private function animate():void
		{
			sprBat.play("fly");
		}
		
	}
}